<template>
	<view class="type">
		<u-navbar>
			<view class="u-nav-slot" slot="left">

				<view class="type-search" @click="toSearch">
					<view class="type-search__inps">
						<image class="type-search__inps__icon" mode="widthFix"
							:src="$imgUrl('/index/search/icon-search.png')">
						</image>
						搜索
					</view>
				</view>

			</view>
		</u-navbar>
		<!-- 固定搜索框 -->

		<view style="position: fixed;background-color: #fff;z-index: 999;left: 0;right: 0;"
			:style="{top:statusBarHeight+'px'}">
			<u-tabs :list="firstTab" :scrollable="true" lineColor="#2FCC71" @click="changeFirst"></u-tabs>
		</view>

		<!-- 列表 -->
		<view class="type-body" :style="{paddingTop:statusBarHeight+58+'px'}">
			<view class="type-body__type" :style="{top:statusBarHeight+58+'px'}">
				<view v-for="(item,index) in typeList" :key="index" class="type-body__type__part"
					:class="activeType === item.id ? 'active' : ''" @click="selectType(item.id,index)">
					{{ item.name }}
					<view class="type-body__type__line" v-if="activeType === item.id"></view>
				</view>
			</view>
			<view class="type-body__list">
				<!-- 列表 -->
				<goods-list :listData="listData" :ishome="true"></goods-list>
			</view>
		</view>

		<!-- 固底内容 -->
		<view class="shopcar-topay" v-if="isLogin == 1 && userInfo.authStatus == 1">
			<!-- <view class="title" v-if="(jisuan(amount, 1)-amount) > 0">
				<view class="titleleft">
					<image class="image" src="https://bhzhtc.oss-cn-beijing.aliyuncs.com/sxsd/704dd6f8a2b14bccad11bfc1bf69624f.png" mode=""></image>
					<view>再买{{(jisuan(amount, 1)-amount).toFixed(2)}}元可<span style="color: #FB5152;font-size: 24rpx;">减{{jisuan(amount, 2)}}元</span></view>
				</view>
				<view class="titleright" @click="tourl">去凑单 ></view>
			</view>
			<view style="height: 80rpx" v-else=""></view> -->
			<view class="box">
				<view class="shopcar-topay__left">
					<!-- <image v-if="isSelectAll" class="shopcar-topay__icon" mode="widthFix"
						:src="$imgUrl('/shopcar/icon-select.png')"></image>
					<image v-else class="shopcar-topay__icon" mode="widthFix" :src="$imgUrl('/shopcar/icon-noselect.png')">
					</image>
					全选 -->
					<image class="type-shopcar__icon" mode="widthFix" :src="$imgUrl('/type/icon-shopcar.png')"></image>
					<!-- <view class="type-shopcar__number" v-if="shopCart.count > 0">{{ shopCart.count }}</view> -->
				</view>
				<view class="shopcar-topay__right">
					<!-- {{ count }}件, -->
					<view class="shopcar-topay__right__text"><span class="unit">¥</span><span
							class="price">{{ amount.toFixed(2) }}</span>
						<!-- <view style="margin-left: 24rpx;" v-if="amount">已减{{ jianamount(amount) }}元</view> -->
					</view>
					<view class="shopcar-topay__right__btn" @click="toCreateOrder" v-if="amountlist.baoyou <= amount">
						去结算
					</view>
					<view v-else="" style="color: #666666">差¥{{(amountlist.baoyou-amount).toFixed(2)}}起送</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import goodsList from '@/components/typeList.vue'
	import {
		mapState,
		mapActions
	} from 'vuex';
	import {
		accAdd,
		accMul
	} from '@/utils/index.js'
	export default {
		components: {
			goodsList
		},
		data() {
			return {
				firstTab: [],
				typeList: [],
				activeType: null,
				listData: [],
				loadMore: false,
				pageNum: 1,
				pageSize: 20,
				amountlist: {},
				typeIndex: 0,
				statusBarHeight: 88,
			}
		},
		computed: {
			...mapState(['isLogin', 'userInfo', 'shopCart']),

			count() {
				let count = 0
				if (this.shopCart.detail) {
					for (let i = 0; i < this.shopCart.detail.length; i++) {
						if (this.shopCart.detail[i].checked == 1) {
							count += this.shopCart.detail[i].count
						}
					}
				}
				return count
			},
			amount() {
				let amount = 0

				if (this.shopCart.detail) {
					for (let i = 0; i < this.shopCart.detail.length; i++) {
						if (this.shopCart.detail[i].checked == 1) {
							if (this.userInfo.vip == 0) {
								amount = accAdd(amount, accMul(this.shopCart.detail[i].count, this.shopCart.detail[i]
									.unitPrice))
							} else {
								amount = accAdd(amount, accMul(this.shopCart.detail[i].count, this.shopCart.detail[i]
									.vip))
							}
						}
					}
				}
				return amount
			},
			isSelectAll() {
				let flag = true
				for (let i = 0; i < this.shopCart.detail.length; i++) {
					if (this.shopCart.detail[i].checked != 1) {
						flag = false
					}
				}
				return flag
			}
		},
		onLoad() {
			console.log(uni.$u.sys());
			this.statusBarHeight = uni.$u.sys().statusBarHeight + 44
			if (!uni.getStorageSync('openid')) {
				uni.navigateTo({
					url: '/pages/login/login'
				})
				return
			}


			// this.getTypeList()
			this.$apis.goods.goodsAmount().then(res => {
				console.log(res)
				this.amountlist = res.data
			})
			this.getUserInfo()
		},
		onShow() {
			if (this.userInfo.authStatus == 1) {
				this.getShopCart()
			}
			this.getFirstTab()
		},
		// 下拉刷新
		onPullDownRefresh() {
			uni.showNavigationBarLoading()
			this.refresh()
			uni.hideNavigationBarLoading()
			uni.stopPullDownRefresh()
		},
		// 上拉加载更多
		onReachBottom() {
			if (this.loadMore) {
				this.pageNum = this.pageNum + 1
				this.getList()
			}
			// else {
			// 	if (this.typeIndex < this.typeList.length) {

			// 		uni.showLoading({
			// 			title: '加载中...',
			// 			mask: true,

			// 		})
			// 		setTimeout(() => {
			// 			let index = this.typeIndex + 1
			// 			this.selectType(this.typeList[index].id, index)
			// 		}, 1000)

			// 	}
			// }
		},
		methods: {

			...mapActions(['getShopCart', 'setCreateOrderData', 'getUserInfo']),
			// 获取商品类别
			async getTypeList(type) {
				const data = await this.$apis.goods.goodsType({
					type
				})
				this.typeList = data.data
				this.selectType(data.data[0].id, 0)
			},
			async getFirstTab() {
				const data = await this.$apis.goods.getFirst()
				this.firstTab = data.data
				this.getTypeList(data.data[0].type)
			},
			changeFirst(e) {
				this.getTypeList(e.type)
			},
			jianamount(amount) {
				var price = 0
				if (this.amountlist && this.amountlist.manjians == undefined) {
					console.log(123)
					return 0
				}
				try {
					this.amountlist.manjians.forEach((item, index) => {
						if (amount >= item.man) {
							price = item.jian
							throw new Error('error')
						}
					})
				} catch (e) {
					if (e.message == 'false') {
						return
					}
				}

				return price
			},
			jisuan(amount, type) {
				var price = 0
				if (this.amountlist && this.amountlist.manjians == undefined) {
					console.log(123)
					return 0
				}
				try {
					this.amountlist.manjians.forEach((item, index) => {
						if ((amount + 100) >= item.man) {
							if (type == 1) {
								price = item.man
							} else {
								price = item.jian
							}

							throw new Error('error')
						}
					})
				} catch (e) {
					if (e.message == 'false') {
						return
					}
				}

				return price
			},
			tourl() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			// 去搜索页面
			toSearch() {
				uni.navigateTo({
					url: '/pages/index/search/index'
				})
			},
			// 切换分类
			selectType(id, index) {
				this.typeIndex = index
				this.activeType = id
				console.log(this.activeType);
				this.refresh()
			},
			// 获取
			refresh() {
				this.loadMore = true
				this.listData = []
				this.pageNum = 1
				this.getList()
			},
			// 根据搜索内容keywords匹配列表
			async getList() {
				const data = await this.$apis.goods.goodsList({
					pageNum: this.pageNum,
					pageSize: this.pageSize,
					type: this.activeType
				})
				this.listData = [...this.listData, ...data.rows]
				if (this.listData.length >= data.total) {
					this.loadMore = false

				}
				// uni.hideLoading()
			},
			// 切换到购物车页面
			toShopCar() {
				uni.switchTab({
					url: '/pages/shopcar/index'
				})
			},
			toCreateOrder() {
				if (this.shopCart?.detail?.length === 0) {
					uni.showToast({
						title: '请先添加商品',
						icon: 'none',
						duration: 2000,
						mask: true
					});
					return
				}
				this.setCreateOrderData(this.shopCart.detail)
				// this.$apis.order.createOrder({
				// 	type: 0,
				// 	ids: this.shopCart.detail.map((item, index) => {
				// 		return item.id
				// 	}).join(','),
				// 	delivery: 0,
				// }).then(res => {
				// 	console.log(res, 213123)
				// 	if (res.code == 500) return uni.showToast({
				// 		title: res.msg,
				// 		icon: 'none'
				// 	})
				// 	uni.navigateTo({
				// 		url: `/pages/order/createOrder/index?data=${JSON.stringify(res.data)}`
				// 	})
				// })
				uni.navigateTo({
					url: `/pages/order/createOrder/index?data=${JSON.stringify(this.shopCart.detail.map((item, index) => {
						return item.id
					}).join(','),)}`
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.type ::v-deep .u-tabs__wrapper__nav__item__text {
		font-size: 19px !important;
	}

	.type {

		.type-search {
			// width: 750rpx;
			height: 88rpx;
			background-color: #ffffff;
			// position: fixed;
			// top: 0;
			// left: 0;
			// z-index: 2;
			display: flex;
			align-items: center;
			justify-content: center;

			.type-search__inps {
				width: 510rpx;
				height: 64rpx;
				background: #F5F5F5;
				border-radius: 32rpx;
				display: flex;
				align-items: center;
				font-size: 26rpx;
				color: #999999;
			}

			.type-search__inps__icon {
				margin-left: 24rpx;
				width: 40rpx;
				height: 40rpx;
				display: block;
				margin-right: 10rpx;
			}
		}

		.type-body {
			width: 750rpx;
			// padding-top: calc(88rpx + 44px);
			// padding-top: calc(var(--status-bar-height) + 44px);
			// padding-top: 88rpx;
			padding-bottom: 144rpx;

			.type-body__type {
				position: fixed;
				// top: calc(88rpx + 44px);
				// top: calc(var(--status-bar-height) + 44px);
				// top: 88rpx;
				left: 0;
				bottom: 144rpx;
				// height: calc(100vh - 500rpx);
				overflow-y: auto;

				.type-body__type__part {
					width: 160rpx;
					height: 100rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					background: #F6F6F8;
					position: relative;
					font-size: 26rpx;
					color: #333333;

					&.active {
						background: #ffffff;
					}

					.type-body__type__line {
						width: 6rpx;
						height: 36rpx;
						background: #008702;
						border-radius: 3rpx;
						position: absolute;
						top: 32rpx;
						left: 0;
					}
				}

			}

			.type-body__list {
				width: calc(100% - 160rpx - 32rpx);
				margin-left: 176rpx;
			}
		}

		.shopcar-topay {
			position: fixed;
			left: 0;
			bottom: 0;
			z-index: 5;
			width: 100%;
			height: 100rpx;
			padding: 0 20rpx 0 24rpx;
			display: flex;
			flex-direction: column;
			justify-content: flex-start;
			box-sizing: border-box;
			background-color: #ffffff;

			.title {
				height: 80rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				background-color: #FEF7F6;
				font-size: 24rpx;
				padding: 0 20rpx;
				box-sizing: border-box;

				.titleleft {
					display: flex;
					justify-content: flex-start;
					align-items: center;

					.image {
						width: 136rpx;
						height: 40rpx;
						margin-right: 22rpx;
					}
				}

				.titleright {
					color: #FB4041;
				}
			}

			.box {
				display: flex;
				align-items: center;
				justify-content: space-between;
				background: #FFFFFF;
				height: 96rpx;

				.shopcar-topay__left {
					font-size: 24rpx;
					color: #666666;
					display: flex;
					align-items: center;

					.type-shopcar__icon {
						width: 100rpx;
						height: 100rpx;
					}
				}

				.shopcar-topay__icon {
					width: 44rpx;
					height: 44rpx;
					margin-right: 16rpx;
				}

				.shopcar-topay__right {
					display: flex;
					align-items: center;
					justify-content: space-between;
					flex: 1;
				}

				.shopcar-topay__right__text {
					font-size: 28rpx;
					color: #666666;
					display: flex;
					justify-content: flex-start;
					align-items: center;
					margin-left: 15rpx;

					.unit {
						color: #FB4041;
					}

					.price {
						font-size: 36rpx;
						color: #FB4041;
					}
				}

				.shopcar-topay__right__btn {
					width: 206rpx;
					height: 74rpx;
					background: #FB4041;
					border-radius: 36rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					font-size: 30rpx;
					font-weight: bold;
					color: #FFFFFF;
					margin-left: 16rpx;
				}
			}
		}

	}
</style>